package com.pioneer.web.stock.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pioneer.common.constant.StockConstants;
import com.pioneer.common.exception.CustomException;
import com.pioneer.web.stock.domain.StoInout;
import com.pioneer.web.stock.domain.StoInoutBody;
import com.pioneer.web.stock.domain.StoOnhand;
import com.pioneer.web.stock.mapper.StoOnhandMapper;
import com.pioneer.web.stock.service.IStoOnhandService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * StoOnhandServiceImpl
 *
 * @author hlm
 * @date 2025-04-16 15:03:54
 */
@Service
public class StoOnhandServiceImpl extends ServiceImpl<StoOnhandMapper, StoOnhand> implements IStoOnhandService {

    @Override
    public List<Map<String, Object>> selectOnhandList(StoOnhand onhand) {
        return getBaseMapper().selectOnhandList(onhand);
    }

    @Override
    public boolean updateOnhandByInout(StoInout stoInout) {
        List<StoInoutBody> inoutBodyList = stoInout.getInoutBodyList();
        if (CollUtil.isEmpty(inoutBodyList)) {
            throw new CustomException("出入库单子表不能为空");
        }
        if (StockConstants.INOUT_TYPE_IN.equals(stoInout.getType())) {
            // 入库
            for (StoInoutBody inoutBody : inoutBodyList) {
                // 根据仓库ID和存货ID查询现存量记录
                LambdaQueryWrapper<StoOnhand> query = Wrappers.lambdaQuery();
                query.eq(StoOnhand::getStoreId, inoutBody.getStoreId()).eq(StoOnhand::getInventoryId, inoutBody.getInventoryId());
                StoOnhand onhand = getOne(query);
                if (onhand == null) {
                    // 新增
                    StoOnhand addOnhand = new StoOnhand();
                    addOnhand.setStoreId(inoutBody.getStoreId());
                    addOnhand.setInventoryId(inoutBody.getInventoryId());
                    addOnhand.setOnhandNumber(inoutBody.getOrderNumber());
                    save(addOnhand);
                } else {
                    // 修改
                    StoOnhand updateOnhand = new StoOnhand();
                    updateOnhand.setOnhandId(onhand.getOnhandId());
                    BigDecimal onhandNumber = onhand.getOnhandNumber();
                    if (onhandNumber == null) {
                        onhandNumber = BigDecimal.ZERO;
                    }
                    // 修改现存量=现存量+入库数量
                    updateOnhand.setOnhandNumber(onhandNumber.add(inoutBody.getOrderNumber()));
                    updateById(updateOnhand);
                }
            }
        } else {
            // 出库
            for (int i = 0; i < inoutBodyList.size(); i++) {
                StoInoutBody inoutBody = inoutBodyList.get(i);
                // 根据仓库ID和存货ID查询现存量记录
                LambdaQueryWrapper<StoOnhand> query = Wrappers.lambdaQuery();
                query.eq(StoOnhand::getStoreId, inoutBody.getStoreId()).eq(StoOnhand::getInventoryId, inoutBody.getInventoryId());
                StoOnhand onhand = getOne(query);
                if (onhand == null) {
                    throw new CustomException("现存量为空，无法出库");
                } else {
                    // 获取现存量
                    BigDecimal onhandNumber = onhand.getOnhandNumber();
                    if (onhandNumber == null) {
                        onhandNumber = BigDecimal.ZERO;
                    }
                    // 判断现存量和出库数量
                    if (onhandNumber.doubleValue() < inoutBody.getOrderNumber().doubleValue()) {
                        throw new CustomException("第" + (i + 1) + "行可用量不足，无法出库");
                    }
                    // 修改现存量=现存量-出库数量
                    StoOnhand updateOnhand = new StoOnhand();
                    updateOnhand.setOnhandId(onhand.getOnhandId());
                    updateOnhand.setOnhandNumber(onhandNumber.subtract(inoutBody.getOrderNumber()));
                    updateById(updateOnhand);
                }
            }
        }
        return true;
    }
}
